package tech.mobi.child.adapter;

import java.util.ArrayList;
import java.util.List;

import tech.mobi.child.CZDAClientListTxtViewMainAct;
import tech.mobi.child.R;
import tech.mobi.child.bean.JYGYClientBean;
import tech.mobi.child.utils.AsyncImageLoader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class JYGYClientListAdapter<T> extends CustomAdapter {
	private final static String TAG = "TuanCanListAdapter";
	private ListView listView;
	private AsyncImageLoader asyncImageLoader;
	private Context context;
	private List<JYGYClientBean> data;
	private LayoutInflater inflater;
	

	@SuppressWarnings("unchecked")
	public JYGYClientListAdapter(ArrayList<T> data, Context context) {
		super(data, context);
	}

	@SuppressWarnings("unchecked")
	public JYGYClientListAdapter(ArrayList<T> data, ListView listView,
			Context context) {
		super(data, context);

		this.listView = listView;
		asyncImageLoader = new AsyncImageLoader();
		this.context = context;
		this.data = (List<JYGYClientBean>) data;
		inflater = ((Activity) context).getLayoutInflater();
	}

	@SuppressWarnings("unchecked")
	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) context;
		Log.i(TAG, "position----" + position);
		// Inflate the views from XML
		View rowView = convertView;
		ViewHolder viewCache;
		if (rowView == null) {
			rowView = inflater.inflate(R.layout.czda_client_txt_items, null);
			viewCache = new ViewHolder();
//			viewCache.image = (ImageView) rowView.findViewById(R.id.image);
			viewCache.title = (TextView) rowView.findViewById(R.id.title);
			viewCache.info = (TextView) rowView.findViewById(R.id.info);
//			viewCache.price = (TextView) rowView.findViewById(R.id.price);
//			viewCache.price2 = (TextView) rowView.findViewById(R.id.price2);
//			viewCache.people = (TextView) rowView.findViewById(R.id.people);
			// viewCache.releaseID = (TextView)
			// rowView.findViewById(R.id.releaseID);
			rowView.setTag(viewCache);
		} else {
			viewCache = (ViewHolder) rowView.getTag();
		}
		JYGYClientBean mealInfo = (JYGYClientBean) getItem(position);

		// Load the image and set it on the ImageView

////		String imageUrl = mealInfo.getImgUrl();
//		String imageUrl = mealInfo.getImagezoom();
//		ImageView imageView = viewCache.image;
//		imageView.setTag(imageUrl);
//
//		String path = asyncImageLoader.loadDrawable(imageUrl,
//				activity.getCacheDir(), new ImageCallback() {
//					public void imageLoaded(String imagePath, String imageUrl) {
//						ImageView imageViewByTag = (ImageView) listView
//								.findViewWithTag(imageUrl);
//						if (imageViewByTag != null && imagePath != null
//								&& !"".equals(imagePath)) {
////							Bitmap bitmap = FileUtil.getBitmapOfCompress(
////									imagePath, 200 * 200);
//							File file = new File(imagePath);
//							Bitmap bitmap = null;
//							if(file.exists()){
//								//BitmapFactory.Options opt = new BitmapFactory.Options();
//								//opt.inJustDecodeBounds = true;
//								bitmap = BitmapFactory.decodeFile(file.getPath());
//							}
//
//							if (bitmap != null)
//								imageViewByTag.setImageBitmap(bitmap);
//						}
//					}
//				});
//		if (path == null || "".equals(path)) {
//			imageView.setImageResource(R.drawable.desklogo);
//		} else {
////			Bitmap bitmap = FileUtil.getBitmapOfCompress(path, 200 * 200);
//			Bitmap bitmap = BitmapFactory.decodeFile(path);
//			if (bitmap != null) {
//				imageView.setImageBitmap(bitmap);
//			} else {
//				imageView.setImageResource(R.drawable.desklogo);
//			}
//		}
		// Set the text on the TextView
		viewCache.title.setText(mealInfo.getTitle());
		viewCache.info.setText(mealInfo.getContent()); //
//		viewCache.price.setText(mealInfo.getPrice() + "元");
//		viewCache.price2.setText(mealInfo.getMarketPrice() + "元 ");
//		viewCache.people.setText(mealInfo.getXs_data() + "人");
//		// viewCache.button.setOnClickListener((GroupListNewAct)context);
//		// viewCache.button.setTag(mealInfo);
//		// viewCache.releaseID.setText(mealInfo.getReleaseId());
//		viewCache.price2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
//		viewCache.price2.setTag(mealInfo);
		viewCache.title.setTag(mealInfo);
		rowView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				JYGYClientBean groupBean = (JYGYClientBean) ((TextView) v
						.findViewById(R.id.title)).getTag();
//				Session session = Session.getSession();
//				if (session.get("goodsData") != null) {
//					session.cleanUpSession();
//					session.remove("goodsData");
//				}
//				session.put("goodsData", groupBean);
				
				Intent intent = new Intent(context, CZDAClientListTxtViewMainAct.class);
				intent.putExtra("sId", groupBean.getId());
				intent.putExtra("sTitle", groupBean.getTitle());
				intent.putExtra("sContent", groupBean.getContent());
				context.startActivity(intent);
				
//				Intent intent = new Intent(context, GoodClientDetailAct.class);
//				intent.putExtra("title", R.string.good_client_detail);
//				intent.putExtra("id", groupBean.getReleaseId());
//				context.startActivity(intent);

			}

		});

		return rowView;
	}

	private class ViewHolder {

//		public ImageView image;
		public TextView title;
		public TextView info;
//		public TextView price;
//		public TextView price2;
//		public TextView people;
		// public TextView releaseID;
	}

	public void setData(ArrayList<JYGYClientBean> data) {
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}